package cn.itcast.service.system.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void save(User user) {
        user.setId(IdUtil.simpleUUID());
        userDao.save(user);
    }

    @Override
    public void update(User user) {
        userDao.update(user);

    }

    @Override
    public void delete(String id) {
        userDao.delete(id);

    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, String companyId) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userDao.findAll(companyId);
        return new PageInfo(userList,5);
    }

    @Override
    public List<String> findRoleIdListByUserId(String id) {
        return userDao.findRoleIdListByUserId(id);
    }

    @Override
    public void changeRole(String userId, String[] roleIds) {
        userDao.deleteRoleIdsByUserId(userId);
        if(roleIds!=null && roleIds.length>0){
            for (String roleId : roleIds) {
                userDao.saveRoleIdAndUserId(userId,roleId);
            }


        }
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public List<User> findByBirthday(String today) {

        return userDao.findByBirthday(today);
    }
}
